package com.ylpms.agme.service;

import com.ylpms.agme.dao.*;
import com.ylpms.agme.entity.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**********************************
 * 指标服务类
 * 1 提供指标、指标等级、指标等级因子、指标等级因子范围的信息维护
 * 2 提供指标运算法服务
 * add by heyao 2019.01
 */
@Service
public class IndexService {

    @Resource
    RegionDao regionDao;

    @Resource
    PubertyDao pubertyDao;

    @Resource
    FarmActDao farmActDao;

    @Resource
    DisasterDao disasterDao;

    @Resource
    IndexDao indexDao;

    @Resource
    IndexLevelDao indexLevelDao;

    @Resource
    IndexLevelFactorDao indexLevelFactorDao;

    @Resource
    IndexLevelFactorRangeDao indexLevelFactorRangeDao;

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());


    public IndexEntity getIndex(String indexType,int regionid,String cropcode,String time){
        //根据指标类型判断
        //如果是生育期指标，则根据cropid和 当前时间time 获取当前生育期，得到生育期编码，从而确定指标项。
        //如果是农业气象灾害类型指标，根据cropid和time获取灾害编码，从而确定指标项
        //如果是农事活动类型指标，根据cropid和time确定当前农事活动编码，从而确定指标项。
        IndexEntity indexEntity = null;
        AgmeBizEntity agmeBizEntity =null;
        if(indexType.equals("QXZH")){//气象灾害指标

        }else if(indexType.equals("SYQ")){//生育期指标
            agmeBizEntity = pubertyDao.getPubertyInfo(regionid,cropcode,time);
        }else if(indexType.equals("BCH")){//病虫害指标

        }else if(indexType.equals("NYQXZH")){//农业气象灾害指标
            agmeBizEntity = farmActDao.getFarmActInfo(regionid,cropcode,time);
        }else if(indexType.equals("NSHD")){//农事活动指标
            agmeBizEntity = disasterDao.getDisasterInfo(regionid,cropcode,time);
        }
        indexEntity = indexDao.findIndexByTypeAndBizCode(indexType,agmeBizEntity.getIndexBizCode());
        if(indexEntity != null){
            //根据指标项，获取指标等级
            List<IndexLevelEntity> indexLevelEntities = indexLevelDao.getIndexLevels(indexEntity.getId());
            indexEntity.setIndexLevelEntities(indexLevelEntities);
            //根据指标等级，获取指标等级因子
            for(IndexLevelEntity indexLevelEntity : indexLevelEntities){
                List<IndexLevelFactorEntity> indexLevelFactorEntities = indexLevelFactorDao.getIndexLevelFactors(indexLevelEntity.getId());
                indexLevelEntity.setIndexLevelFactorEntityArrayList(indexLevelFactorEntities);
                for(IndexLevelFactorEntity indexLevelFactorEntity : indexLevelFactorEntities){
                    List<IndexLevelFactorRangeEntity> indexLevelFactorRangeEntities = indexLevelFactorRangeDao.getIndexLevelFactorRanges(indexLevelFactorEntity.getId());
                    indexLevelFactorEntity.setIndexLevelFactorRangeEntities(indexLevelFactorRangeEntities);
                }
            }
        }
        return indexEntity;
    }
}
